﻿
<div class="ld-tabs">
    <ul>
        <li class="k-state-active">库存列表</li>
    </ul>
    <div>
        <div class="k-block divToolbar">
            <b>库存量总计：</b>
            <span id="spQuantity"></span>（只）
            &nbsp;&nbsp;&nbsp;&nbsp;
            <span id="spWeight"></span>（斤）
        </div>
        <div class="k-block divToolbar">
            <div style="display: inline-block;">
                <input id="selHouse" />
            </div>
            <div style="display: inline-block">
                <input type="text" id="txtSelProduct" data-value="0" />
            </div>
            <div style="display: inline-block;">
                <input id="txtMaterial" data-value="0" />
            </div>
            <div style="display: inline-block;">
                <input id="txtProcess" data-value="0" />
            </div>
            <div style="display: inline-block;">
                <input id="txtSize" data-value="0" />
            </div>
            <button class="k-button" onclick="Filters()"><span class="k-icon k-i-search"></span><span>搜索</span></button>
        </div>
        <div id="grid_stocks" style="text-align: center"></div>
    </div>
</div>

<script class="init">
    LocatedMenu("仓库管理", "库存情况");

    $("#selHouse").kendoDropDownList({
        dataTextField: "House",
        dataValueField: "ID",
        dataSource: Whale.dataSource({
            url: "/Products/Warehouse/Gets"
        })
    });

    var allProducts = Whale.Remote.GetEntity({
        url: "/Products/Product/Gets",
        parameters: {
            pageSize: 0,
            productStatus: 1
        }
    });

    var materials = Whale.Remote.GetEntity({
        url: "/Products/Material/Gets"
    });

    var processes = Whale.Remote.GetEntity({
        url: "/Products/Process/Gets"
    });
    var sizes = Whale.Remote.GetEntity({
        url: "/Products/Size/Gets"
    });
    $("#txtSelProduct").kendoComboBox({
        placeholder: "请选择产品...",
        filter: "contains",
        dataTextField: "Module",
        dataValueField: "ID",
        dataSource: allProducts,
        select: function (e) {
            var item = this.dataItem(e.item.index());
            $("#txtSelProduct").attr("data-value", item.ID);
        },
        change: function (e) {
            if (!$.isNumeric(this.value())) {
                this.text("");
                $("#txtSelProduct").attr("data-value", 0);
            }
        }
    });

    $("#txtMaterial").kendoDropDownList({
        optionLabel: {
            MaterialName: "请选择材料...",
            ID: 0
        },
        dataTextField: "MaterialName",
        dataValueField: "ID",
        dataSource: materials
    });

    $("#txtProcess").kendoDropDownList({
        optionLabel: {
            ProcessName: "请选择加工...",
            ID: 0
        },
        dataTextField: "ProcessName",
        dataValueField: "ID",
        dataSource: processes
    });

    $("#txtSize").kendoDropDownList({
        optionLabel: {
            SizeName: "请选择规格...",
            ID: 0
        },
        dataTextField: "SizeName",
        dataValueField: "ID",
        dataSource: sizes
    });
</script>

<script class="grid_stocks_columns">
    var stockFields = [{
        title: "产品型号",
        field: "Product.Module",
        width: 150,
        sortable: false
    }, {
        title: "材料",
        field: "MaterialName",
        width: 120,
        sortable: false
    }, {
        title: "加工",
        field: "ProcessName",
        width: 80,
        sortable: false
    }, {
        title: "规格",
        field: "Size",
        width: 80,
        sortable: false
    }, {
        title: "库存量",
        field: "Quantity",
        template:"#=QuantityAndUnit#",
        width: 150,
        sortable: true,
        attributes: { style: "text-align:right" }
    }, {
        title: "库存量(斤)",
        field: "Ratio",
        template: "#=ShowWeight(Ratio,Quantity)#",
        width: 150,
        sortable: true,
        attributes: { style: "text-align:right" }
    }, {
        title: "更新日期",
        field: "LastUpdateOn",
        template: "#=kendo.toString(kendo.parseDate(LastUpdateOn),'yyyy-MM-dd')#",
        width: 100,
        sortable: false
    }, {}];

    function ShowWeight(r, q) {
        if (r > 0)
            return (q / r).toFixed(2)  + "(斤)";
        else
            return "";
    }
</script>

<script class="grid_config">
    $(function () {
        $("#grid_stocks").kendoGrid({
            columns: stockFields,
            dataSource: Whale.dataSource({
                url: "/Products/Stock/Gets",
                getParameters: function () {
                    return {
                        productID: $("#txtSelProduct").attr("data-value"),
                        materialID: $("#txtMaterial").data("kendoDropDownList").value(),
                        processID: $("#txtProcess").data("kendoDropDownList").value(),
                        size: $("#txtSize").data("kendoDropDownList").value() > 0 ? $("#txtSize").data("kendoDropDownList").text() : "",
                        getProduct: true,
                        houseID: $("#selHouse").data("kendoDropDownList").value()
                    };
                },
                pageSize: 20,
                serverSorting: true
            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            },
            sortable: true
        });
    });

    function Filters() {
        $("#grid_stocks").data("kendoGrid").dataSource.page(0);
        GetSum();
    }

    function GetSum() {
        var sums = Whale.Remote.GetEntity({
            url: "/Products/Stock/GetSum",
            parameters: {
                productID: $("#txtSelProduct").attr("data-value"),
                materialID: $("#txtMaterial").data("kendoDropDownList").value(),
                processID: $("#txtProcess").data("kendoDropDownList").value(),
                size: $("#txtSize").data("kendoDropDownList").value() > 0 ? $("#txtSize").data("kendoDropDownList").text() : "",
                houseID: $("#selHouse").data("kendoDropDownList").value()
            }
        });
        if (sums != null && sums.length == 2) {
            $("#spQuantity").html(sums[0]);
            $("#spWeight").html(sums[1].toFixed(2));
        }
    }
    GetSum();
</script>

